<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-09-17
 * Time: 14:31
 */

namespace App\Http\Controllers\Api\Shop;

use App\Http\Controllers\Api\BaseAdminController;
use App\Http\Requests\GetGoodsDetailRequest;
use App\Http\Requests\Goods\DeleteGoodsCategoryRequest;
use App\Http\Requests\Goods\DeleteGoodsRequest;
use App\Http\Requests\Goods\GoodsCategoryDetailRequest;
use App\Http\Requests\Goods\UpdateGoodsCategoryRequest;
use App\Http\Requests\Goods\UpdateGoodsStatusRequest;
use App\Http\Requests\Goods\UpdateRecommendGoodsCategoryRequest;
use App\Http\Requests\Goods\UpdateStatusGoodsCategoryRequest;
use App\Http\Transformers\Goods\GetGoodsDetailTransformer;
use App\Http\Transformers\Goods\GoodsCategoryDetailTransformer;
use App\Repositories\GoodsRepository;
use App\Http\Transformers\Goods\GetGoodsSkuTransformer;
use App\Http\Transformers\Goods\GetGoodsCategoryPageTransformer;
use App\Http\Transformers\Goods\GetGoodsPageTransformer;
use App\Http\Transformers\Goods\GetGoodsListTransformer;
use App\Transformers\Admin\AddGoodsRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

/**
 * 与商品有关的
 * Class GoodsController
 * @package App\Http\Controllers\Api\Goods
 */
class GoodsController extends BaseAdminController
{
    /**
     * @var GoodsRepository 注入的外部服务
     */
    protected $goodsRepository;

    public function __construct(GoodsRepository $goodsRepository){
        $this->goodsRepository = $goodsRepository;
    }

    /**
     * 添加分类
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     */
    public function addGoodsCategory(Request $request){
        $data = $request->all(['name', 'parentId', 'sort', 'sortNo',
            'pic', 'isRecommend', 'status']);

        data_set($data, 'createBy', Auth::user()->nickname );
        $add = $this->goodsRepository->addGoodsCategoryRepository($data);

        return $this->response->accepted('', 'success');
    }

    /**
     * 更新商品分类
     * @param UpdateGoodsCategoryRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function updateGoodsCategory(UpdateGoodsCategoryRequest $request){
        $data = $request->all(['name', 'parentId', 'sort', 'sortNo',
            'pic', 'isRecommend', 'status', 'detail']);
        $categoryId = $request->get('categoryId');

        data_set($data, 'createBy', Auth::user()->nickname );

        if( $this->goodsRepository->updateGoodsCategoryRepository($data, $categoryId) ){
            return $this->response->accepted('', 'success');
        }

        $this->response->errorInternal('更新失败！');
    }

    /**
     * 删除商品分类
     * @param DeleteGoodsCategoryRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function deleteGoodsCategory(DeleteGoodsCategoryRequest $request){
        $categoryId = $request->get('categoryId');

        if($this->goodsRepository->checkHasGoodsInGoodsCategory($categoryId)){
            $this->response->errorBadRequest('该分类下含有商品，不能删除！');
        }

        $delete = $this->goodsRepository->deleteGoodsCategoryRepository($categoryId);

        if($delete){
            return $this->response->accepted('', 'success');
        }

        $this->response->errorInternal('删除失败！');
    }

    /**
     * 获取分类信息详情
     * @param GoodsCategoryDetailRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function goodsCategoryDetail(GoodsCategoryDetailRequest $request){
        $categoryId = $request->get('categoryId');

        $detail = $this->goodsRepository->goodsCategoryDetail($categoryId);

        return $this->response->item($detail, new GoodsCategoryDetailTransformer());
    }

    /**
     * 更新推荐状态
     * @param UpdateRecommendGoodsCategoryRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function updateRecommendGoodsCategory(UpdateRecommendGoodsCategoryRequest $request){
        $data = $request->all(['categoryId', 'isRecommend']);

        $update = $this->goodsRepository->updateRecommendGoodsCategoryRepository($data);

        return $this->response->accepted('', 'success');
    }

    /**
     * 更新显示状态
     * @param UpdateStatusGoodsCategoryRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function updateStatusGoodsCategory(UpdateStatusGoodsCategoryRequest $request){
        $data = $request->all(['categoryId', 'status']);

        $update = $this->goodsRepository->updateRecommendGoodsCategoryRepository($data);

        return $this->response->accepted('', 'success');
    }

    /**
     * 获取商品分类分页列表
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     */
    public function getCategoryPage(Request $request){
        $limit = $request->get('limit');
        $data = $request->except(['limit', 'page']);

//        data_set($data, 'status', $data['status'] ?? 0);
//        var_dump($limit, $data);exit;

        $list = $this->goodsRepository->getCategoryPageRepository($limit, $data);

        return $this->response->paginator($list, new GetGoodsCategoryPageTransformer());
    }

    /**
     * 获取条件分类
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     */
    public function getCategoryList(Request $request){
        $list = $this->goodsRepository->goodsListRepository();

        return $this->response->collection($list, new GetGoodsListTransformer());
    }

    /**
     * 添加商品
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     */
    public function addGoods(AddGoodsRequest $request){
        $data = $request->all();

        if($startTime = data_get($data, 'startTime') && $endTime = data_get($data, 'endTime')){
            if($endTime <= $startTime){
                $this->response->errorBadRequest('结拍时间不能小于开拍时间');
            }
        }

        data_set($data, 'createBy', Auth::user()->nickname );
        $add = $this->goodsRepository->addGoodsRepository($data);

        return $this->response->accepted('', 'success');
    }

    /**
     * 获取商品分页列表 不含拍卖
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     */
    public function getGoodsPage(Request $request){
        $limit = $request->get('limit', 0);
        $data = $request->except(['limit', 'page']);

        $page = $this->goodsRepository->getGoodsPageRepository($data, $limit);

        return $this->response->paginator($page, new GetGoodsPageTransformer());
    }

    /**
     * 获取商品详细信息
     * @param GetGoodsDetailRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function getGoodsDetail(GetGoodsDetailRequest $request){
        $id = $request->get('id');

        $detail = $this->goodsRepository->getGoodsDetailRepository($id);

        return $this->response->item($detail, new GetGoodsDetailTransformer());
    }

    /**
     * 删除商品
     * @param DeleteGoodsRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function deleteGoods(DeleteGoodsRequest $request){
        $ids = $request->get('ids');

        $delete = $this->goodsRepository->deleteGoodsRepository($ids);

        if($delete){
            return $this->response->accepted('', 'success');
        }

        $this->response->errorInternal('删除失败！');
    }

    /**
     * 更新商品
     * @param UpdateGoodsStatusRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function updateGoods(UpdateGoodsStatusRequest $request){
        $all = $request->all();
        $id = $request->get('id');

        $data = $this->goodsRepository->updateGoodsRepository($id, $all);

        return $this->response->item($data, new GetGoodsDetailTransformer());
    }

    /**
     * 更新状态及推荐
     * @param UpdateGoodsStatusRequest $request
     * @return \Dingo\Api\Http\Response
     */
    public function updateGoodsStatus(UpdateGoodsStatusRequest $request){
        $all = $request->all();
        $ids = $request->get('ids');

        $data = $this->goodsRepository->updateGoodsStatusRepository($ids, $all);

        return $this->response->accepted('', 'success');
    }

    /**
     * 获取商品的sku列表
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     */
    public function getGoodsSku(Request $request){
        $goodsId = $request->id;
        $keyword = $request->get('keyword');

        $page = $this->goodsRepository->getGoodsSkuRepository($goodsId,$keyword);

        return $this->response->collection($page, new GetGoodsSkuTransformer());
    }

    /**
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     */
    public function updateGoodsSku(Request $request){
        if( !$data = $request->all() ){
            $this->response->errorBadRequest('缺少更新参数');
        }

        $this->goodsRepository->updateGoodsSkuRepository($data);

        return $this->response->accepted('', 'success');
    }

    /**
     * 保存拍卖信息
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     */
    public function addAuctionInfo(Request $request){
        $data = $request->all();

        if($startTime = data_get($data, 'startTime') && $endTime = data_get($data, 'endTime')){
            if($endTime <= $startTime){
                $this->response->errorBadRequest('结拍时间不能小于开拍时间');
            }
        }

        $add = $this->goodsRepository->addAuctionInfoRepository($data);

        return $this->response->accepted('', 'success');
    }

}
